<template>
  <div class="login-content">
    <div class="login-dialog">
      <div class="logo-box">
        <img src="../../assets/logo.png" />
      </div>
      <el-form
        class="loginFormBox"
        @submit.native.prevent="handleLogin"
        @keyup.enter.native="handleLogin"
      >
        <div class="loginInputBox">
          <el-input
            class="loginInput"
            v-model="username"
            clearable
            placeholder="请输入用户名"
            :prefix-icon="User"
            autocomplete="off"
          ></el-input>
        </div>
        <div class="loginInputBox">
          <el-input
            class="loginInput"
            v-model="password"
            clearable
            :prefix-icon="Lock"
            type="password"
            autocomplete="off"
            show-password
            placeholder="请输入密码"
          ></el-input>
        </div>
        <div class="login-btn" :class="{ 'login-btn-disabled': loginDisabled }">
          <el-button @click.stop="handleLogin" :loading="isLogining"
            >{{ isLogining ? '登录中' : '登录' }}
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { Lock, User } from '@element-plus/icons-vue'
import { ref, computed, nextTick } from 'vue'
import loginApi from '@/api/loginApi.js'
import { encryptPassword } from '@/utils/cryptoUtils.js'
import message from '@/utils/elMessage.js'
import { setToken } from '@/utils/auth.js'
import { useUserStore } from '@/stores/userInfo.js'
import { usePublicStore } from '@/stores/publicInfo.js'
import { useRouter } from 'vue-router'

const router = useRouter()
const userStore = useUserStore()
const publicStore = usePublicStore()

// const username = ref('admin')
// const password = ref('Qwerasdfzxc.12')

const username = ref('')
const password = ref('')
const isLogining = ref(false)
const loginDisabled = computed(
  () => isLogining.value || username.value === '' || password.value === '',
)

async function gotoHome() {
  const fullLoading = message.fullLoading('正在跳转到项目首页...')
  try {
    // 并行请求提高速度
    await Promise.all([
      userStore.initUserData(), // 合并用户信息和菜单获取
      publicStore.getMiddleTable(),
    ])

    // 确保路由已添加完成
    await nextTick()
    await router.push('/')
  } catch (error) {
    console.error('跳转首页失败:', error)
    message.error(error.message || '系统初始化失败')
    // 可以跳转到登录页或错误页
    await router.push('/login')
  } finally {
    fullLoading.close()
  }
}

function handleLogin() {
  if (username.value === '') {
    message.error('请输入用户名!')
    return
  }
  if (password.value === '') {
    message.error('请输入密码!')
    return
  }
  isLogining.value = true
  const params = {
    t: +new Date(),
    username: username.value,
    password: encryptPassword(password.value),
  }
  loginApi
    .login(params)
    .then((res) => {
      const { code, token } = res
      if (code === 0) {
        if (token) {
          setToken(token)
          //跳转到项目首页
          gotoHome()
        }
      } else {
        message.warning(res.msg || '登录失败！')
      }
    })
    .catch((error) => {
      console.error(error.message)
    })
    .finally(() => {
      isLogining.value = false
    })
}
</script>

<style scoped lang="scss">
@media (max-width: 900px) {
  .login-content {
    background-image: none !important;
    background-color: #fff !important;
    min-height: 500px !important;
  }

  .login-dialog {
    top: 50% !important;
    left: 50% !important;
    right: auto !important;
    margin-top: -255px;
    margin-left: -180px;
    box-shadow: none !important;
    padding: 20px 40px !important;
  }

  .el-message {
    max-width: 200px !important;
    min-width: 200px !important;
  }
}

.login-content {
  position: relative;
  width: 100%;
  height: 100%;
  background: #c1d1f2 url('../../assets/login_bg1.png') no-repeat 50%;
  background-size: cover !important;

  .login-dialog {
    position: fixed;
    padding: 40px 45px 60px;
    margin-top: -255px;
    top: 50%;
    right: 279px;
    width: 360px;
    height: 510px;
    border-radius: 10px;
    background-color: #fff;
    box-shadow: 6px 6px 24px rgba(165, 189, 235, 0.8);
    opacity: 1;
    transition: all 0.5s;
  }

  .logo-box {
    height: 115px;
    line-height: 115px;
    text-align: center;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .loginFormBox {
    margin-top: 40px;
    position: relative;
  }

  .loginInputBox {
    position: relative;
    margin-bottom: 40px;
  }

  .login-btn {
    &.login-btn-disabled {
      opacity: 0.5;
    }

    .el-button {
      background-color: $theme-color;
      font-size: 16px;
      font-weight: 700;
      color: #fff;
      height: 44px;
      width: 100%;
    }
  }
}
</style>
